import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';

typedef Widget BuildFormStatusWidget(bool enable);

class FormStatusWidget extends StatelessWidget {
  final Widget enable;
  final Widget disable;
  final BuildFormStatusWidget builder;

  const FormStatusWidget({Key key, this.enable, this.disable, this.builder})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (builder != null)
      return builder(FormEditStatusInheritedWidget.ofEnable(context));

    return FormEditStatusInheritedWidget.ofEnable(context) ? enable : disable;
  }
}

class FormEditStatusInheritedWidget extends InheritedWidget {
  final bool isEdit;

  FormEditStatusInheritedWidget({Widget child, this.isEdit})
      : super(child: child);

  static FormEditStatusInheritedWidget of(BuildContext context) {
    return context
        .dependOnInheritedWidgetOfExactType<FormEditStatusInheritedWidget>();
  }

  static TextStyle ofStyle(BuildContext context, [bool useDisable]) {
    if (useDisable == true) {
      return TextStyle(
        color: aceBlackColor,
        fontSize: getSp(16),
        fontWeight: FontWeight.w700,
        height: 14 / 16,
      );
    }
    FormEditStatusInheritedWidget status = of(context);
    if (status == null) {
      return TextStyle(
        color: aceBlueColor,
        fontSize: getSp(16),
        fontWeight: FontWeight.w700,
        height: 14 / 16,
      );
    } else {
      return status.textStyle;
    }
  }

  static bool ofEnable(BuildContext context) {
    FormEditStatusInheritedWidget status = of(context);
    if (status == null) {
      return true;
    } else {
      return status.isEdit;
    }
  }

  TextStyle get textStyle => isEdit == true
      ? TextStyle(
          color: aceBlueColor,
          fontSize: getSp(16),
          fontWeight: FontWeight.w700,
          height: 14 / 16,
        )
      : TextStyle(
          color: aceBlackColor,
          fontSize: getSp(16),
          fontWeight: FontWeight.w700,
          height: 14 / 16,
        );

  @override
  bool updateShouldNotify(covariant FormEditStatusInheritedWidget oldWidget) {
    return isEdit != oldWidget.isEdit;
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<bool>('isEdit', isEdit));
  }
}
